#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.ComponentModel;
#endregion

namespace WindowsGame1.Components
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MenuScreen : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public MenuScreen(Game game)
            : base(game)
        {
        }

        ContentManager content;

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            base.Initialize();
            content= new ContentManager(Game.Services);
        }

        public string SpriteFontAssetName { get; set; }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            MouseState state = Mouse.GetState();
            float left = 50;
            SpriteFont font = content.Load<SpriteFont>(SpriteFontAssetName);
            foreach (MenuItem mi in items)
            {
                Vector2 fontVector = font.MeasureString(mi.Name);
                Rectangle r = new Rectangle((int)left, (int)(Game.Window.ClientBounds.Height-fontVector.Y), (int)fontVector.X, (int)fontVector.Y);
                if (r.Contains(state.X, state.Y))
                {
                    if (state.LeftButton == ButtonState.Pressed)
                        mi.State = MenuItemState.Down;
                    else
                        mi.State = MenuItemState.Over;
                }
                else
                    mi.State = MenuItemState.Normal;
                left += fontVector.X * 2;
            }
        }

        private MenuItemCollection items = new MenuItemCollection();

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public MenuItemCollection MenuItems
        {
            get { return items; }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public MenuItem SelectedMenuItem
        {
            get { return items.SelectedMenuItem; }
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sprite = new SpriteBatch(GraphicsDevice);
            SpriteFont font = content.Load<SpriteFont>(SpriteFontAssetName);
            sprite.Begin();
            float left = 50;
            foreach (MenuItem mi in items)
            {
                Vector2 fontVector = font.MeasureString(mi.Name);
                Color c;
                switch (mi.State)
                {
                    case MenuItemState.Normal:
                        c=normalColor;
                        break;
                    case MenuItemState.Over:
                        c=hoverColor;
                        break;
                    case MenuItemState.Down:
                        c=downColor;
                        break;
                    default:
                        c=Color.LightGoldenrodYellow;
                        break;
                }

                sprite.DrawString(font, mi.Name, new Vector2(left, Game.Window.ClientBounds.Height-fontVector.Y), c);
                left += fontVector.X * 2;
            }
            sprite.End();
            base.Draw(gameTime);
        }

        private Color normalColor = Color.LightGreen;

        public Vector3 NormalColor
        {
            get { return normalColor.ToVector3(); }
            set { normalColor = new Color(value); }
        }

        private Color hoverColor = Color.LightPink;

        public Vector3 HoverColor
        {
            get { return hoverColor.ToVector3(); }
            set { hoverColor = new Color(value); }
        }

        private Color downColor = Color.LightGray;

        public Vector3 DownColor
        {
            get { return downColor.ToVector3(); }
            set { downColor = new Color(value); }
        }

    }

    public class MenuItemCollection:System.Collections.ObjectModel.Collection<MenuItem>
    {
        private IList<MenuItem> list=new List<MenuItem>();

        public void Add(string menuItemName)
        {
            Add(new MenuItem(menuItemName));
        }

        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; }
        }

        public MenuItem SelectedMenuItem
        {
            get { return list[selectedIndex]; }
        }
    }

    public enum MenuItemState { Normal, Over, Down }

    public delegate void UpdateState(object sender, EventArgs e);

    public class MenuItem
    {
        public event UpdateState StateChanged;

        public MenuItem()
        {
        }

        public MenuItem(string name)
        {
            this.name=name;
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private int index;

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int ItemIndex
        {
            get { return index; }
            set { index = value; }
        }

        private MenuItemState state=MenuItemState.Normal;

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public MenuItemState State
        {
            get { return state; }
            set
            {
                state = value;
                if (StateChanged != null)
                    StateChanged(this, new EventArgs());
            }
        }

        public bool Selected
        {
            get { return state == MenuItemState.Over; }
        }
    }
}


